﻿# encoding:utf-8
# 访问数据库 消息
from wxpyImport import *
from db.wxpyDbBase import *
    
# 保存聊天记录 
def dbAddMsg(dbd, msg, resetDb=False):
    #logInfo('in dbAddMs')
    
    #数据库message存表消息最大长度
    const_msg_max_length = 64
    
    try:
        #个人消息
        sender = msg.sender
        receiver = msg.receiver
        receiverType = 1     #个人
        is_at = 0
        text64 = str(msg.text)[0:const_msg_max_length-1] #只保存前64个字符
        
        #别人发消息到群
        if isGroup(msg.sender):
            sender = msg.member
            receiver = msg.sender
            receiverType = 2     #群
            if msg.is_at:
                is_at = 1
                
        #自己发消息到群
        if isGroup(msg.receiver):
            sender = msg.member
            receiver = msg.receiver
            receiverType = 2     #群
            is_at = 0

        sql = "INSERT INTO message (msg_id,type,senderPuid,receiverPuid,receiverType,is_at\
            ,create_time,receive_time, text) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        sqlBig = "INSERT INTO message_big (msg_id,text,raw) VALUES(%s,%s,%s)"
    except Exception as e:
        dealException()
        logError('dbAddMs err: '+str(e))

    try:
        # 貌似腾讯新闻类的消息没有 sender.puid
        senderPuid = sender.puid
        if senderPuid is None:
            logError('senderPuid empty: '+str(sender.raw))
            senderPuid = ''
            
        receiverPuid = receiver.puid
        if receiverPuid is None:
            logError('receiverPuid empty: '+str(receiver.raw))
            receiverPuid = ''
            
        cursor = getCursor(dbd, resetDb)
        # 存聊天记录
        cursor.execute(sql,(msg.id, msg.type, senderPuid, receiverPuid, receiverType, is_at\
            , msg.create_time, msg.receive_time, text64))
        # 存大块聊天记录
        # if msg.type==msgType.TEXT or msg.type==msgType.PICTURE:
        text = msg.text
        if msg.text is None:
            text = ''
        textRaw = '{}'
        # 20190726 修改未验证
        if hasattr(msg,'raw'):
            textRaw = str(msg.raw)
        cursor.execute(sqlBig,(msg.id, text, textRaw))
            
        dbd.db.commit()
        cursor.close()
    except Exception as e:
        dealException()
        logError('dbAddMs err2: '+str(e))
        if not resetDb: # 防止死循环
            return dbAddMsg(dbd, msg, True)
    else:
        logInfo('insert msg success: ' + str(msg.id))        

    #logInfo('out dbAddMs')

# 更新消息中的图片 url
# msg: 整条消息结构
def dbUpdateMsgImg(msg, url):
    sql = "update message_big set img_url = %s, file_name=%s, file_size=%s where msg_id = %s"

    dbd = dbData()
    try:
        cursor = getCursor(dbd)
        file_name = '' if ''==msg.file_name else msg.file_name
        file_size = 0 if ''==msg.file_size else int(msg.file_size)
        # 存大块聊天记录
        cursor.execute(sql,(url, file_name, file_size, msg.id))
        dbd.db.commit()
        cursor.close()
    except Exception as e:
        dealException()
        logError('dbUpdateMsgIm err: '+str(e))
    else:
        logInfo('dbUpdateMsgIm success: ' + url)
    finally:
        closeDb(dbd)